package com.xuecheng.auth.controller;

import com.xuecheng.ucenter.model.po.XcUser;
import com.xuecheng.ucenter.service.WxAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

/**
 * @author: Hes
 * @description 微信回调接口
 * @date: 2023/6/26 16:49
 */
@Controller
@Slf4j
@Api(value = "微信回调接口", tags = "微信回调接口")
public class WxLoginController {
    @Autowired
    WxAuthService wxAuthService;

    @ApiOperation("微信回调接口")
    @RequestMapping("/wxLogin")
    public String wxLogin(String code, String state) throws IOException {
        log.debug("微信扫码回调,code:{},state:{}", code, state);
        //请求微信申请令牌，拿到令牌查询用户信息，将用户信息写入本项目数据库
//        XcUser xcUser = new XcUser();
//        //暂时硬编写，目的是调试环境
//        xcUser.setUsername("t1");
        //调用微信认证接口，获取用户信息
        XcUser xcUser = wxAuthService.weAuth(code);
        if (xcUser == null) {
            return "redirect:http://www.51xuecheng.cn/error.html";
        }
        String username = xcUser.getUsername();
        return "redirect:http://www.51xuecheng.cn/sign.html?username=" + username + "&authType=wx";
    }

}
